package com.example.event_resource_java.authority.controller.resourceLibrary;


import com.example.event_resource_java.authority.dao.EventResourceAllocationDO;
import com.example.event_resource_java.authority.domain.resourceLibrary.SecretKey;
import com.example.event_resource_java.authority.server.resourceLibrary.SecretKeyService;
import com.example.event_resource_java.commons.enums.EnumErrorCode;
import com.example.event_resource_java.commons.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2024-02-28 09:32:18 | lance</small>
 */
@CrossOrigin
@Controller
@RequestMapping("/secretKey")
@Api(value = "秘钥管理", tags = {"秘钥管理"})
public class SecretKeyController {


    /**
     * 服务对象
     */

    private final SecretKeyService secretKeyService;

    public SecretKeyController(SecretKeyService secretKeyService) {
        this.secretKeyService = secretKeyService;
    }

    @ApiOperation(value = "密钥管理页面地址", notes = "密钥管理页面地址")
    @GetMapping("keyManagement")
    @RequiresPermissions("jzweb:secretKey:keyManagement")
    String keyManagement() {
        return "jzweb/keyManagement/keyManagement";
    }


    @ApiOperation(value = "密钥管理新增页面地址", notes = "密钥管理新增页面地址")
    @GetMapping("add/")
    @RequiresPermissions("jzweb:secretKey:add")
    String add(Model model) {
        Map<String, List<EventResourceAllocationDO>> map = secretKeyService.getAddSecretKy();
        model.addAttribute("map", map);
        return "jzweb/keyManagement/add";
    }


    @ApiOperation(value = "密钥管理编辑页面地址", notes = "密钥管理编辑页面地址")
    @GetMapping("edit/{id}")
    @RequiresPermissions("jzweb:secretKey:edit")
    String edit(Model model, @PathVariable("id") Long id) {
        SecretKey secretKey = secretKeyService.getSecretKy(id);
        model.addAttribute("secretKey", secretKey);
        return "jzweb/keyManagement/edit";
    }


    /**
     * 新增数据
     *
     * @param secretKey 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增秘钥", notes = "新增秘钥")
    @ResponseBody
    @PostMapping("/saveSecretKey")
    public ApiResult<Object> saveSecretKey(@RequestBody SecretKey secretKey) {
        return this.secretKeyService.saveSecretKey(secretKey);
    }

    /**
     * 修改数据
     *
     * @param secretKey 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改秘钥", notes = "修改秘钥")
    @ResponseBody
    @PostMapping("/editSecretKey")
    public ApiResult<Object> editSecretKey(@RequestBody SecretKey secretKey) {
        return this.secretKeyService.editSecretKey(secretKey);
    }

    /**
     * 删除数据
     *
     * @param id 结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除秘钥", notes = "删除秘钥")
    @ResponseBody
    @GetMapping(value = "/deletedById")
    public ApiResult<Object> deletedById(Long id) {
        return this.secretKeyService.removeById(id);
    }


    @ApiOperation(value = "多条件列表分页查询", notes = "多条件列表分页查询")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = SecretKey.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/getSecretKyPage")
    public ApiResult<Object> getSecretKyPage(@RequestBody SecretKey secretKey) {
        return this.secretKeyService.getSecretKyPage(secretKey);
    }

    @ApiOperation(value = "定时任务测试", notes = "定时任务测试")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = SecretKey.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @PostMapping(value = "/timingEffect")
    public ApiResult<Object> timingEffect() {
        this.secretKeyService.timingEffect();
        return ApiResult.success();
    }


    @ApiOperation(value = "根据资源id，用户id验证是否有查看资源权限", notes = "根据资源id，用户id验证是否有查看资源权限")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = SecretKey.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/permissionVerification")
    public ApiResult<Object> permissionVerification(@RequestParam("allocationId") String allocationId) {
        ApiResult<Object> apiResult = new ApiResult<>();
//        try {
            Boolean b = this.secretKeyService.permissionVerification(Long.parseLong(allocationId));
            apiResult.setSuccess(b);
            apiResult.setCode(EnumErrorCode.success.getCode());
            apiResult.setMsg(EnumErrorCode.success.getMsg());
//        } catch (Exception e) {
//            apiResult.setCode(1);
//			apiResult.setData(false);
//            return apiResult;
//        }
        return apiResult;
    }

    @ApiOperation(value = "用户id验证是否有全部资源权限", notes = "根用户id验证是否有全部资源权限")
    @ApiResponses({
            @ApiResponse(code = 0, message = "操作成功", response = SecretKey.class),
            @ApiResponse(code = 500, message = "服务器内部异常")})
    @ResponseBody
    @GetMapping(value = "/purchaseAll")
    public ApiResult<Object> purchaseAll() {
        Boolean isAll = this.secretKeyService.purchaseAll();
        return ApiResult.success(isAll);
    }

}
